package com.piece.core.web.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "spring.security")
public class SecurityProperties {

    /**
     * 监控中心和swagger需要访问的url
     */
    private static final String[] ENDPOINTS = {
            "/css/**",
            "/js/**",
            "/image/**",
            "/images/**",
            "/lib/**",
            "/fonts/**",
            "/*/v2/api-docs",
            "/doc.html",
            "/swagger/api-docs",
            "/swagger-ui.html",
            "/swagger-ui/index.html",
            "/swagger-resources/**",
            "/webjars/**",
            "/site/**",
            "/oauth/**",
            "/actuator/**"
    };

    public Process process = new Process();
    public Session session = new Session();
    public Cookie cookie = new Cookie();
    public Token token = new Token();
    public Validation validation = new Validation();

    // 不用认证的url
    public String[] getIgnoreUrl() {
        List<String> list = new ArrayList<>();
        list.add(process.getLoginUrl());
        list.add(process.getLoginProcessUrl());
        list.add(process.getRegisterUrl());
        list.add(process.getRegisterProcessUrl());
        list.add(process.getValidateCodeProcessingUrl());

        if (process.getIgnored() != null) {
            list.addAll(Arrays.asList(ENDPOINTS));

            String[] ignores = process.getIgnored();
            for (String url : ignores) {
                list.add(url);
            }
        }

        return list.toArray(new String[list.size()]);
    }

    @Data
    public class Process {
        // 自定义白名单
        private String[] ignored;

        // 登录地址
        private String loginUrl = "/authentication/login";

        private String loginProcessUrl = "/authentication/doLogin";

        // 注册地址
        private String registerUrl = "/authentication/register";

        private String registerProcessUrl = "/authentication/doRegister";

        // 验证码处理地址
        private String validateCodeProcessingUrl = "/authentication/code/**";

        // 退出地址
        private String logoutUrl = "/logout";
    }

    @Data
    public class Session {
        // 失效时间（分钟）
        private int expire = 30;
    }

    @Data
    public class Cookie {
        // 失效时间（天）
        private int expire = 30;
    }

    @Data
    public class Token {
        // token自动续签
        private boolean renew = true;
        // 同时可登陆数量
        private int maximum = 1;
        // 默认通用密码
        private String password = "12345";
        // 保存类型
        private String storeType = "redis";
        // 失效时间（分钟）
        private int expire = 60 * 24 * 7;
    }

    @Data
    public class Validation {
        // 密码错误重试次数
        private int retry = 3;
        // 密码错误禁用时间(分钟)
        private int interval = 10;
    }
}
